<?php

namespace app\models;

use app\admin\service\CommonService;
use think\Model;

/**
 * Class UserModel.
 */
class UserModel extends Model
{
    protected $table = 'edu_user';

    /**
     * @param $param
     * @return array
     * @throws \think\exception\DbException
     */
    public function getLists($param)
    {
        $page = isset($param['page']) ? (int) $param['page'] : 1;
        $listRows = isset($param['limit']) ? (int) $param['limit'] : 10;
        $where = [];
        $listData = [];
        if (isset($param['word']) && '' !== $param['word']) {
            $where['name|mobile'] = ['like', $param['word'].'%'];

            if(get_current_type() == 4 || get_current_type() == 5){
                $where['campus_id'] = get_current_campus_id();
            }else{
                if(get_current_type() == 2){ // 集团账号
                    $where['wtype'] = array('neq',1);  //非超管账号
                    $where['company_id'] = get_current_company_id(); //集团ID
                }

                if(get_current_type() == 3) { // 区域账号
                    $where['wtype'] = ['in','1,2'];  //非超管账号,非集团
                    $where['company_id'] = get_current_company_id(); //集团ID
                }
            }

            $listData = self::where($where)
                ->paginate($listRows, false, ['page' => $page])
                ->toArray();
            if (!empty($listData)) {
                foreach ($listData['data'] as &$item) {
                    $item['campus_name'] = '';
                    $item['mobile'] = $item['mobile'] ? $item['mobile'] : '-';
                    $campus_arr = explode(',',$item['campus_id']);
                    for($i =0 ; $i < count($campus_arr); $i ++){
                      $item['campus_name'] = $item['campus_name'].CampusModel::get(['id' => $campus_arr[$i]])['campus_name'].'-';
                    }

                }
            }
        }
        return $listData;
    }

    /**
     * 用户管理.
     *
     * @param $param
     *
     * @throws \think\exception\DbException
     *
     * @return array
     */
    public function getUserLists($param)
    {
        $page = isset($param['page']) ? (int) $param['page'] : 1;
        $listRows = isset($param['limit']) ? (int) $param['limit'] : 10;
        $where = [];
        if (isset($param['username']) && '' !== $param['username']) {
            $where['username'] = ['like', $param['username'].'%'];
        }
        if (isset($param['company_id']) && '' !== $param['company_id']) {
            $where['company_id'] = $param['company_id'];
        }
        if (isset($param['status']) && '' !== $param['status']) {
            $where['status'] = $param['status'];
        }
        if (isset($param['status_center']) && '' !== $param['status_center']) {
            $where['status_center'] = $param['status_center'];
        }
        if (isset($param['mobile']) && '' !== $param['mobile']) {
            $where['mobile'] = ['like', '%'.$param['mobile'].'%'];
        }
		$where['wtype'] = 2;
		$where['group_id'] = ['neq',1];
        if(get_current_type() != 1){
            $where['company_id'] = get_current_company_id();
        }
        $listData = self::where($where)
			->order('id desc')
            ->paginate($listRows, false, ['page' => $page])
            ->toArray();
        if (!empty($listData)) {
            foreach ($listData['data'] as &$item) {
                $item['status_text'] = (1 === $item['status']) ? '启用中' : '禁用中';
                $item['wx_status'] = (1 === $item['status_center']) ? '<span  style="color:#01AAED;font-weight: bold;">已绑定</span>' : '<span  style="color:#FF5722;font-weight: bold;">未绑定</span>';
                $item['gender'] = (1 === $item['gender']) ? '男' : '女';
                $item['mobile'] = $item['mobile'] ? $item['mobile'] : '-';

                if($item['group_type'] == 1){
                    $item['group_type_text'] = '一般类属性';
                }else if($item['group_type'] == 2){
                    $item['group_type_text'] = '老师类属性';
                }else if($item['group_type'] == 3){
                    $item['group_type_text'] = '助教类属性';
                }else if($item['group_type'] == 4){
                    $item['group_type_text'] = '班主任/学管师类属性';
                }else if($item['group_type'] == 5){
                    $item['group_type_text'] = '教务类属性';
                }

                $item['title'] = AuthModel::getById($item['group_id'], 'title');
                $item['company_name'] = CompanyModel::get($item['company_id'])['company_name'];
                $item['groupId'] = get_current_group_id();
				if (!$item['img']) {
					$item['img'] = '<img src="'.'/img.jpg'.'" style="width:30px;height:30px; border-radius:15px;">';
				} else {
					$item['img'] = '<img src="'.$item['img'].'" style="width:30px;height:30px; border-radius:15px;">';
				}
            }
        }

        return $listData;
    }

    /**
     * 区域用户管理.
     *
     * @param $param
     *
     * @throws \think\exception\DbException
     *
     * @return array
     */
    public function getRegionalUserLists($param)
    {
        $page = isset($param['page']) ? (int) $param['page'] : 1;
        $listRows = isset($param['limit']) ? (int) $param['limit'] : 10;
        $where = [];
        if (isset($param['username']) && '' !== $param['username']) {
            $where['username'] = ['like', $param['username'].'%'];
        }
        if (isset($param['status']) && '' !== $param['status']) {
            $where['status'] = $param['status'];
        }

        if (isset($param['status_center']) && '' !== $param['status_center']) {
            $where['status_center'] = $param['status_center'];
        }
        if (isset($param['mobile']) && '' !== $param['mobile']) {
            $where['mobile'] = ['like', '%'.$param['mobile'].'%'];
        }

		$where['wtype'] = $param['wtype'];
		if(get_current_type() != 1){
			$where['company_id'] = get_current_company_id();
		}
		$where['id'] = ['neq',get_current_id()];
		$listData = self::where($where)
			->order('id desc')
            ->paginate($listRows, false, ['page' => $page])
            ->toArray();
        if (!empty($listData)) {
            foreach ($listData['data'] as &$item) {
                $item['status_text'] = (1 === $item['status']) ? '启用中' : '禁用中';
                $item['wx_status'] = (1 === $item['status_center']) ?  '<span  style="color:#01AAED;font-weight: bold;">已绑定</span>' : '<span  style="color:#FF5722;font-weight: bold;">未绑定</span>';
                $item['gender'] = (1 === $item['gender']) ? '男' : '女';
                $item['mobile'] = $item['mobile'] ? $item['mobile'] : '-';

                if($item['group_type'] == 1){
                    $item['group_type_text'] = '一般类属性';
                }else if($item['group_type'] == 2){
                    $item['group_type_text'] = '老师类属性';
                }else if($item['group_type'] == 3){
                    $item['group_type_text'] = '助教类属性';
                }else if($item['group_type'] == 4){
                    $item['group_type_text'] = '班主任/学管师类属性';
                }else if($item['group_type'] == 5){
                    $item['group_type_text'] = '教务类属性';
                }

                $item['title'] = AuthModel::getById($item['group_id'], 'title');
                $item['groupId'] = get_current_group_id();
				if (!$item['img']) {
					$item['img'] = '<img src="'.'/img.jpg'.'" style="width:30px;height:30px; border-radius:15px;">';
				} else {
					$item['img'] = '<img src="'.$item['img'].'" style="width:30px;height:30px; border-radius:15px;">';
				}
            }
        }

        return $listData;
    }

	/**
	 * 校区用户管理.
	 *
	 * @param $param
	 *
	 * @throws \think\exception\DbException
	 *
	 * @return array
	 */
	public function getCampusUserLists($param)
	{
		$page = isset($param['page']) ? (int) $param['page'] : 1;
		$listRows = isset($param['limit']) ? (int) $param['limit'] : 10;
		$where = [];
		if (isset($param['username']) && '' !== $param['username']) {
			$where['username'] = ['like', $param['username'].'%'];
		}
		if (isset($param['status']) && '' !== $param['status']) {
			$where['status'] = $param['status'];
		}

        if (isset($param['status_center']) && '' !== $param['status_center']) {
            $where['status_center'] = $param['status_center'];
        }
        if (isset($param['mobile']) && '' !== $param['mobile']) {
            $where['mobile'] = ['like', '%'.$param['mobile'].'%'];
        }


        $where['wtype'] = $param['wtype'];
		if(get_current_type() != 1){
            $where['company_id'] = get_current_company_id();
		}
		$where['id'] = ['neq',get_current_id()];
		$listData = self::where($where)
			->order('id desc')
			->paginate($listRows, false, ['page' => $page])
			->toArray();
		if (!empty($listData)) {
			foreach ($listData['data'] as &$item) {
				$item['status_text'] = (1 === $item['status']) ? '启用中' : '禁用中';
                $item['wx_status'] = (1 === $item['status_center']) ? '<span  style="color:#01AAED;font-weight: bold;">已绑定</span>' : '<span  style="color:#FF5722;font-weight: bold;">未绑定</span>';
				$item['gender'] = (1 === $item['gender']) ? '男' : '女';
				$item['mobile'] = $item['mobile'] ? $item['mobile'] : '-';

                if($item['group_type'] == 1){
                    $item['group_type_text'] = '一般类属性';
                }else if($item['group_type'] == 2){
                    $item['group_type_text'] = '老师类属性';
                }else if($item['group_type'] == 3){
                    $item['group_type_text'] = '助教类属性';
                }else if($item['group_type'] == 4){
                    $item['group_type_text'] = '班主任/学管师类属性';
                }else if($item['group_type'] == 5){
                    $item['group_type_text'] = '教务类属性';
                }

                $item['title'] = AuthModel::getById($item['group_id'], 'title');
				$item['groupId'] = get_current_group_id();
				if (!$item['img']) {
					$item['img'] = '<img src="'.'/img.jpg'.'" style="width:30px;height:30px; border-radius:15px;">';
				} else {
					$item['img'] = '<img src="'.$item['img'].'" style="width:30px;height:30px; border-radius:15px;">';
				}
			}
		}

		return $listData;
	}


    /**
     * 班主任课消管理.
     *
     * @param $param
     *
     * @throws \think\exception\DbException
     *
     * @return array
     */
    public function getHeadMasterUserLists($param)
    {
        $GradesDetailsModel = new GradesDetailsModel();
        $StudentSubjectInfosClassModel = new StudentSubjectInfosClassModel();
        $CourseModel = new CourseModel();
        $StudentSubjectClassModel = new StudentSubjectClassModel();
        $page = isset($param['page']) ? (int) $param['page'] : 1;
        $listRows = isset($param['limit']) ? (int) $param['limit'] : 10;
        $where = [];
        $where['group_type'] = 4; //班主任类

        if (isset($param['campus_id']) && '' !== $param['campus_id']) {   //选择校区
            $where['campus_id'] = $param['campus_id'];
        }


        //班主任
        if (isset($param['headmaster_name']) && '' !== $param['headmaster_name']) {
            $where['name'] = ['like', '%'.$param['headmaster_name'].'%'];
        }


        if(get_current_type() != 1){
            $where['company_id'] = get_current_company_id();
        }

        //1v1
        $course_id_1v1_arr = $CourseModel->where(['course_type' => 41])->column('id');

        //1vN
        $course_id_1vn_arr = $CourseModel->where(['course_type' => 43])->column('id');

        //班课
        $course_id_class_arr = $CourseModel->where(['course_type' => 42])->column('id');



        $where_elimination_1v1 = [];
        $where_elimination_1v1['status'] = 1;
        $where_elimination_1v1['give_status'] = 1;


        $where_elimination_1v1_arrears = [];
        $where_elimination_1v1_arrears['status'] = 1;
        $where_elimination_1v1_arrears['give_status'] = 1;
        $where_elimination_1v1_arrears['arrears_status'] = 1;


        $where_elimination_1vn = [];
        $where_elimination_1vn['status'] = 1;
        $where_elimination_1vn['give_status'] = 1;


        $where_elimination_1vn_arrears = [];
        $where_elimination_1vn_arrears['status'] = 1;
        $where_elimination_1vn_arrears['give_status'] = 1;
        $where_elimination_1vn_arrears['arrears_status'] = 1;


        $where_elimination_class = [];
        $where_elimination_class['status'] = 1;
        $where_elimination_class['give_status'] = 1;


        $where_elimination_class_arrears = [];
        $where_elimination_class_arrears['status'] = 1;
        $where_elimination_class_arrears['give_status'] = 1;
        $where_elimination_class_arrears['arrears_status'] = 1;


        //时间段
        if (isset($param['start_date_elimination']) && '' !== $param['start_date_elimination'] && isset($param['end_date_elimination']) && '' !== $param['end_date_elimination']) {
            $where_elimination_1v1['class_date'] = array('between', array($param['start_date_elimination'],$param['end_date_elimination']));
            $where_elimination_1v1_arrears['class_date'] = array('between', array($param['start_date_elimination'],$param['end_date_elimination']));
            $where_elimination_1vn['class_date'] = array('between', array($param['start_date_elimination'],$param['end_date_elimination']));
            $where_elimination_1vn_arrears['class_date'] = array('between', array($param['start_date_elimination'],$param['end_date_elimination']));
            $where_elimination_class['class_date'] = array('between', array($param['start_date_elimination'],$param['end_date_elimination']));
            $where_elimination_class_arrears['class_date'] = array('between', array($param['start_date_elimination'],$param['end_date_elimination']));
        }

        $listData = self::where($where)
            ->paginate($listRows, false, ['page' => $page])
            ->toArray();
        if (!empty($listData)) {
            foreach ($listData['data'] as &$item) {
                    $item['headmaster_name'] = $item['name'];
                    $item['grades_nums']  = $GradesDetailsModel->where(['user_master_id' => $item['id']])->count();
                    $grades_id_arr = $GradesDetailsModel->where(['user_master_id' => $item['id']])->column('id');
                    $student_id_arr = $StudentSubjectClassModel->where(['class_subject_id' => implode(',',$grades_id_arr)])->column('student_id');
                    $student_id_arr = array_unique($student_id_arr);
                    $item['student_nums'] = count($student_id_arr);

                    //1v1
                    $grades_id_1v1_arr = $GradesDetailsModel->where(['course_id' => ['in', implode(',',$course_id_1v1_arr)],'user_master_id' => $item['id']])->column('id');
                    $grades_id_1v1_arr_str = implode(',',$grades_id_1v1_arr);


                    //1V1总课消
                    $where_elimination_1v1['class_subject_id'] = ['in', $grades_id_1v1_arr_str];
                    $res_total_1v1_amount = $StudentSubjectInfosClassModel->where($where_elimination_1v1)->sum('amount_deducted');  //费用
                    $res_total_1v1_amount = round(abs($res_total_1v1_amount / 100),2);

                    $res_total_1v1_nums = $StudentSubjectInfosClassModel->where($where_elimination_1v1)->count();  //次数


                    $res_total_1v1_duration = $StudentSubjectInfosClassModel->where($where_elimination_1v1)->sum('duration');  //时长
                    $res_total_1v1_duration = $res_total_1v1_duration / 60; //小时

                    $item['class_elimination_one'] = $res_total_1v1_amount.'(元)/'.$res_total_1v1_nums.'(人次)'.'/'.$res_total_1v1_duration.'(小时)';


                    //1V1欠费课消
                    $where_elimination_1v1['class_subject_id'] = ['in', $grades_id_1v1_arr_str];
                    $res_arrears_total_1v1_amount = $StudentSubjectInfosClassModel->where($where_elimination_1v1_arrears)->sum('arrears_amount');  //费用
                    $res_arrears_total_1v1_amount = round(abs($res_arrears_total_1v1_amount / 100),2);

                    $res_arrears_total_1v1_nums = $StudentSubjectInfosClassModel->where($where_elimination_1v1_arrears)->count();  //次数


                    $res_arrears_total_1v1_duration = $StudentSubjectInfosClassModel->where($where_elimination_1v1_arrears)->sum('duration');  //时长
                    $res_arrears_total_1v1_duration = $res_arrears_total_1v1_duration / 60; //小时

                    $item['class_elimination_one_arrears'] = $res_arrears_total_1v1_amount.'(元)/'.$res_arrears_total_1v1_nums.'(人次)'.'/'.$res_arrears_total_1v1_duration.'(小时)';





                    //1vn
                    $grades_id_1vn_arr = $GradesDetailsModel->where(['course_id' => ['in', implode(',',$course_id_1vn_arr),'user_master_id' => $item['id']]])->column('id');
                    $grades_id_1vn_arr_str = implode(',',$grades_id_1vn_arr);

                    //1Vn总课消
                    $where_elimination_1vn['class_subject_id'] = ['in', $grades_id_1vn_arr_str];
                    $res_total_1vn_amount = $StudentSubjectInfosClassModel->where($where_elimination_1vn)->sum('amount_deducted');  //费用
                    $res_total_1vn_amount = round(abs($res_total_1vn_amount / 100),2);

                    $res_total_1vn_nums = $StudentSubjectInfosClassModel->where($where_elimination_1vn)->count();  //次数


                    $res_total_1vn_duration = $StudentSubjectInfosClassModel->where($where_elimination_1vn)->sum('duration');  //时长
                    $res_total_1vn_duration = $res_total_1vn_duration / 60; //小时

                    $item['class_elimination_n'] = $res_total_1vn_amount.'(元)/'.$res_total_1vn_nums.'(人次)'.'/'.$res_total_1vn_duration.'(小时)';


                    //1VN欠费课消
                    $where_elimination_1vn_arrears['class_subject_id'] = ['in', $grades_id_1vn_arr_str];
                    $res_arrears_total_1vn_amount = $StudentSubjectInfosClassModel->where($where_elimination_1vn_arrears)->sum('arrears_amount');  //费用
                    $res_arrears_total_1vn_amount = round(abs($res_arrears_total_1vn_amount / 100),2);

                    $res_arrears_total_1vn_nums = $StudentSubjectInfosClassModel->where($where_elimination_1vn_arrears)->count();  //次数


                    $res_arrears_total_1vn_duration = $StudentSubjectInfosClassModel->where($where_elimination_1vn_arrears)->sum('duration');  //时长
                    $res_arrears_total_1vn_duration = $res_arrears_total_1vn_duration / 60; //小时

                    $item['class_elimination_n_arrears'] = $res_arrears_total_1vn_amount.'(元)/'.$res_arrears_total_1vn_nums.'(人次)'.'/'.$res_arrears_total_1vn_duration.'(小时)';



                    //班课
                    $grades_id_class_arr = $GradesDetailsModel->where(['course_id' => ['in', implode(',',$course_id_class_arr),'user_master_id' => $item['id']]])->column('id');
                    $grades_id_class_arr_str = implode(',',$grades_id_class_arr);


                    //班课总课消
                    $where_elimination_class['class_subject_id'] = ['in', $grades_id_class_arr_str];
                    $res_total_class_amount = $StudentSubjectInfosClassModel->where($where_elimination_class)->sum('amount_deducted');  //费用
                    $res_total_class_amount = round(abs($res_total_class_amount / 100),2);

                    $res_total_class_nums = $StudentSubjectInfosClassModel->where($where_elimination_class)->count();  //次数


                    $res_total_class_duration = $StudentSubjectInfosClassModel->where($where_elimination_class)->sum('duration');  //时长
                    $res_total_class_duration = $res_total_class_duration / 60; //小时

                    $item['class_elimination'] = $res_total_class_amount.'(元)/'.$res_total_class_nums.'(人次)'.'/'.$res_total_class_duration.'(小时)';


                    //班课欠费课消
                    $where_elimination_class_arrears['class_subject_id'] = ['in', $grades_id_class_arr_str];
                    $res_arrears_total_class_amount = $StudentSubjectInfosClassModel->where($where_elimination_class_arrears)->sum('arrears_amount');  //费用
                    $res_arrears_total_class_amount = round(abs($res_arrears_total_class_amount / 100),2);

                    $res_arrears_total_class_nums = $StudentSubjectInfosClassModel->where($where_elimination_class_arrears)->count();  //次数


                    $res_arrears_total_class_duration = $StudentSubjectInfosClassModel->where($where_elimination_class_arrears)->sum('duration');  //时长
                    $res_arrears_total_class_duration = $res_arrears_total_class_duration / 60; //小时

                    $item['class_elimination_arrears'] = $res_arrears_total_class_amount.'(元)/'.$res_arrears_total_class_nums.'(人次)'.'/'.$res_arrears_total_class_duration.'(小时)';

                    //总课消
                    $class_elimination_total = $res_total_1v1_amount + $res_total_1vn_amount + $res_total_class_amount;
                    $class_elimination_nums_total = $res_total_1v1_nums + $res_total_1vn_nums + $res_total_class_nums;
                    $class_elimination_duration_total = $res_total_1v1_duration + $res_total_1vn_duration + $res_total_class_duration;
                    $item['class_elimination_total'] = $class_elimination_total.'(元)/'.$class_elimination_nums_total.'(人次)'.'/'.$class_elimination_duration_total.'(小时)';

                    //总欠费课消
                    $class_elimination_total_arrears = $res_arrears_total_1v1_amount + $res_arrears_total_1vn_amount + $res_arrears_total_class_amount;
                    $class_elimination_nums_total_arrears = $res_arrears_total_1v1_nums + $res_arrears_total_1vn_nums + $res_arrears_total_class_nums;
                    $class_elimination_duration_total_arrears = $res_arrears_total_1v1_duration + $res_arrears_total_1vn_duration + $res_arrears_total_class_duration;
                    $item['class_elimination_total_arrears'] = $class_elimination_total_arrears.'(元)/'.$class_elimination_nums_total_arrears.'(人次)'.'/'.$class_elimination_duration_total_arrears.'(小时)';

            }
        }

        return $listData;
    }

}
